﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using LuckDrawSample.Lib;
using Microsoft.AspNetCore.Mvc;
using LuckDrawSample.Models;

namespace LuckDrawSample.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public List<OutputDto> GetResult()
        {
            var prizeTemplate = PrizeHelper.InitializePrize();
            int index = -1;
            var list = PrizeHelper.ReRatePrize(prizeTemplate, index);
            var weightRate = 1 / list.Sum(x => x.Rate);
            var countQuantity = list.Sum(x => x.Quantity);
            var result = new List<OutputDto>();
            prizeTemplate.ForEach(x =>
            {
                result.Add(new OutputDto()
                {
                    type = 0,
                    edata = new OutputDataDto()
                    {
                        name = x.Name,
                        xAxisIndex = 0,
                        yAxisIndex = 0,
                        type = "line",
                        data = new List<double>()
                    }
                });
                result.Add(new OutputDto()
                {
                    type = 1,
                    edata = new OutputDataDto()
                    {
                        name = x.Name,
                        type = "line",
                        xAxisIndex = 1,
                        yAxisIndex = 1,
                        data = new List<double>()
                    }
                });
            });
            var pmax = 0;
            for (int i = 0; i < countQuantity; i++)
            {
                var method = new AliasMethod(list.Select(x => x.Rate * weightRate).ToList());
                index = method.Next();
                list = PrizeHelper.ReRatePrize(prizeTemplate, index).ToList();
                //每1000轮抽取一次结果用于UI呈现
                if (pmax == 0 || pmax == 1000)
                {
                    pmax = 0;
                    result.ForEach(t =>
                    {
                        var fd = list.FirstOrDefault(y => y.Name == t.edata.name);
                        switch (t.type)
                        {
                            case 0:
                                var refd = result.FirstOrDefault(y => y.edata.name == t.edata.name && t.type == 0);
                                if (fd != null)
                                {
                                    refd.edata.data.Add(Math.Round(fd.Rate * weightRate * 100, 4,MidpointRounding.AwayFromZero));
                                }
                                else
                                {
                                    refd.edata.data.Add(0);
                                }
                                break;
                            case 1:
                                if (fd != null)
                                {
                                    t.edata.data.Add(fd.Quantity);
                                }
                                else
                                {
                                    t.edata.data.Add(0);
                                }
                                break;
                        }
                    });
                }
                pmax++;
            }
            return result;
        }
    }
}
